<template>
	<view>
		<view>{{gcname}}</view>
		<form @submit="save">
			<view class="formlist">
				<view class="ln"><input name="names" :value="htdata.names" placeholder="输入合同名称" /></view>
				<picker :range="tplist" :value="htdata.tp" @change="seltp">
					<view class="ln">{{htType[htdata.tp]}}</view>
				</picker>
				<view class="ln"><input name="supplier" :value="htdata.supplier" placeholder="输入供应商" /></view>
				<view class="ln"><input name="phone" :value="htdata.phone" placeholder="输入联系电话" /></view>
				<view class="ln"><input name="username" :value="htdata.username" placeholder="输入联系人" /></view>
				<view class="ln"><input name="goods" :value="htdata.goods" placeholder="输入商品名" /></view>
				<view class="ln"><input name="amount" :value="htdata.amount" placeholder="输入金额" /></view>
				<picker mode="date" :value="htdata.tm" @change="seltm">
					<view class="ln">{{htdata.tm}}</view>
				</picker>
				<view class="ln"><input name="num" :value="htdata.num" placeholder="输入合同数量" /></view>
				<view class="ln"><input name="operator" :value="htdata.operator" placeholder="输入经办人" /></view>
				<view class="ln"><button form-type="submit">保存</button></view>
			</view>			
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				htid:0, //合同id
				gcid:0, //工程ID
				gcname:"",
				htdata:{tp:0,tm:"输入签订日期"}
			}
		},
		onLoad(opt){
			this.htid = opt.id|0;
			this.gcid = opt.gcid|0;
			this.gcname = opt.gcname;
			if(this.htid > 0){
				uni.setNavigationBarTitle({
					title:"编辑合同"
				});
				this.loadht();
			}else{
				//console.log("htedit gcid", this.gcid);
				if(!this.gcid){
					uni.switchTab({
						url:"index",
						complete: (e) => {
							//console.log("navigateTo", e);
						}
					});
					return;
				}
				uni.setNavigationBarTitle({
					title:"添加合同"
				});
			}
		},
		methods: {
			loadht(){
				let self = this;
				uni.request({
					url:this.apibase + "htdetail?htid=" + this.htid,
					success: (res) => {
						self.htdata = res.data;
					}
				});
			},
			seltp(e){
				this.htdata.tp = e.detail.value;
			},
			seltm(e){
				this.htdata.tm = e.detail.value;
			},
			save(e){
				let self = this, pd = e.detail.value;
				pd.gcid = this.gcid;
				pd.tp = this.htdata.tp;
				pd.tm = this.htdata.tm;
				uni.request({
					url:this.apibase + (this.htid?"htedit?htid=" + this.htid:"htadd"),
					method:"POST",
					data:pd,
					success: (res) => {
						uni.showToast({
							title:"操作成功",
							success:()=>{
								uni.navigateBack({
									animationDuration:500
								})
							}
						});
					}
				});
			}
		}
	}
</script>

<style>

</style>
